package net.jumpengine.ui;


import java.awt.Point;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import net.jumpengine.Manager;
import net.jumpengine.bean.Control;
import net.jumpengine.enume.MouseState;
import net.jumpengine.po.Border;
import net.jumpengine.util.UDraw;

public class ButtonBase extends Control{
	private Point p;
	
	private int r,cx,cy,cwidth,cheight;

	public ButtonBase(int x, int y, int width, int height,String text) {
		this(x, y, width, height,new Border(),text);
	}
	
	public ButtonBase(int x, int y, int width, int height,int index,String text) {
		this(x, y, width, height,new Border(),text);
		super.index=index;
	}
	
	public ButtonBase(int x, int y, int width, int height,Border border,String text) {
		this(x, y, width, height,border,text,Manager.getBackColor(),Manager.getColor());
	}
	
	public ButtonBase(int x, int y, int width, int height,Border border,String text,Color backColor,Color color) {
		super(x, y, width, height, border);
		this.backColor=backColor;
		this.color=color;
		this.text=text;
		this.index=1;
	}

	@Override
	public void draw(GraphicsContext g) {
		if(!visible)return;
		r=border==null?0:border.getRadius();
		if(border!=null)g.setFill(mouseState==MouseState.mouseOut?border.getColor():border.getMoveIn());
		if(r==0) {
			g.fillRect(x, y, width, height);
		}else {
			g.fillRoundRect(x, y, width, height, r, r);
		}
		
		g.setFill(backColor);
		
		if(change) {
			cx=x+(border==null?0:border.getLeft());
			cy=y+(border==null?0:border.getTop());
			cwidth= width-(border==null?0:border.getRight())-(border==null?0:border.getLeft());
			cheight=height-(border==null?0:border.getBottom())-(border==null?0:border.getTop());
		}
		
		if(r==0) {
			g.fillRect(cx,cy,cwidth,cheight);
		}else {
			g.fillRoundRect(cx,cy, cwidth, cheight, r, r);
		}
		
		if(change) {
			p = UDraw.centerString(text, font, cwidth, cheight);
			change=false;
		}
		g.setFill(color);
		g.setFont(font);
		UDraw.drawString(g, text, cx+p.x, cy+p.y);
	}
}
